package cn.huanzi.qch.baseadmin.license;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用于获取客户Mac服务器的基本信息
 *
 * @author zifangsky
 * @date 2018/4/23
 * @since 1.0.0
 */
public class MacServerInfos extends AbstractServerInfos {

    @Override
    protected List<String> getIpAddress() throws Exception {
        List<String> result = null;

        //获取所有网络接口
        List<InetAddress> inetAddresses = getLocalAllInetAddress();

        if(inetAddresses != null && inetAddresses.size() > 0){
            result = inetAddresses.stream().map(InetAddress::getHostAddress).distinct().map(String::toLowerCase).collect(Collectors.toList());
        }

        return result;
    }

    @Override
    protected List<String> getMacAddress() throws Exception {
        List<String> result = null;

        //1. 获取所有网络接口
        List<InetAddress> inetAddresses = getLocalAllInetAddress();

        if(inetAddresses != null && inetAddresses.size() > 0){
            //2. 获取所有网络接口的Mac地址
            result = inetAddresses.stream().map(this::getMacByInetAddress).distinct().collect(Collectors.toList());
        }

        return result;
    }

    @Override
    protected String getCPUSerial() throws Exception {
        //序列号
        String serialNumber = "";

        //使用dmidecode命令获取CPU序列号
        String[] shell = {"sysctl","-n","machdep.cpu.brand_string"};
        Process process = Runtime.getRuntime().exec(shell);
        process.getOutputStream().close();

        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line = reader.readLine().trim();
        if(StringUtils.isNotBlank(line)){
            serialNumber = line;
        }

        reader.close();
        return serialNumber;
    }

    @Override
    protected String getMainBoardSerial() throws Exception {
        //序列号
        String serialNumber = "";

        String shell = "ioreg -l";

        Process proc = Runtime.getRuntime().exec(shell);
        //获取脚本执行的得到的结果并缓存
        BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));

        String str;
        //读取数据
        while ((str = reader.readLine()) != null) {
            if (str.contains("IOPlatformSerialNumber")) {
                serialNumber = str.split("\" = \"")[1];
            }
        }
        reader.close();
        proc.waitFor();

        return serialNumber.trim().replace("\"", "");
    }
}
